.SUFFIX: .c .cpp .s .o .d .h .inc

ifeq "$(OS)" "Windows_NT"
PATH_CONVERT = $(shell cygpath -m $(1))
else
PATH_CONVERT = $(1)
endif

#
# Project and ARM settings
#

#PRIORITY_OPTION  := fast
#PRIORITY_OPTION  := size
PRIORITY_OPTION  := debug

### ARM core (used by option)
ARMCORE     ?= cortex-a7

# FPU_OPTION  := neon-vfpv4
FPU_OPTION  ?= NO_FPU

### Tool chain
TCHAIN  := $(GNUarm)

#
# MILB Libraries
#



#
# Include paths
#

### Used by the option of gcc
COMMON_CINCPATH :=
CMD_CINCPATH    :=
PERI_CINCPATH   :=
OS_CINCPATH     :=
FJ_CINCPATH     := -I$(ROOT_DIR)/boot/include
FJ_CINCPATH     += -I$(ROOT_DIR)/MILB_Header/include
FJ_CINCPATH     += -I$(ROOT_DIR)/MILB_Header/include/Storage
FJ_CINCPATH     += -I$(ROOT_DIR)/MILB_Header/include/Top
FJ_CINCPATH     += -I$(ROOT_DIR)/NF/include
FJ_CINCPATH     += -I$(ROOT_DIR)/emmc/include
### DELETE_TO_NO_WARP
FJ_CINCPATH     += -I$(ROOT_DIR)/warp/include
### DELETE_TO_NO_WARP
IP_CINCPATH     :=
HOST_CINCPATH   :=
CINCPATH        := $(SUB_CINCPATH)
CINCPATH        += $(COMMON_CINCPATH) $(CMD_CINCPATH) $(PERI_CINCPATH) $(OS_CINCPATH) $(FJ_CINCPATH) $(IP_CINCPATH) $(HOST_CINCPATH)

### Used by the option of gcc
AINCPATH        := -I$(ROOT_DIR)/boot/include
AINCPATH        += -I$(ROOT_DIR)/MILB_Header/include
AINCPATH        += -I$(ROOT_DIR)/MILB_Header/include/Storage
AINCPATH        += -I$(ROOT_DIR)/MILB_Header/include/Top
AINCPATH        += -I$(ROOT_DIR)/NF/include
AINCPATH        += -I$(ROOT_DIR)/emmc/include


#
# Toolchain options
#

### Compile Option File
include $(ROOT_DIR)/project/boot_strap.opt
include $(ROOT_DIR)/project/MILB_COMMON.opt

### Compiler
CC  := $(TCHAIN)gcc
CFLAGS  := -c
CFLAGS  += -pipe
CFLAGS  += -std=gnu99
CFLAGS  += -fno-builtin
CFLAGS  += -ffreestanding
CFLAGS  += -mtune=cortex-a7
ifeq "$(PRIORITY_OPTION)" "debug"
CFLAGS  += -g3
CFLAGS  += -O0
else
CFLAGS  += -O2
endif
CFLAGS  += -march=armv7-a
ifneq "$(FPU_OPTION)" "NO_FPU"
CFLAGS  += -mfpu=neon-vfpv4
CFLAGS  += -mfloat-abi=softfp
else
CFLAGS  += -mfloat-abi=soft
endif
CFLAGS  += -mthumb-interwork
CFLAGS  += -mstructure-size-boundary=8
CFLAGS  += -ffreestanding
CFLAGS  += -fsigned-char
CFLAGS  += -Wall
CFLAGS  += $(MILB_COMMON_OPT)
CFLAGS  += $(BOOT_STRAP_OPT)

CFLAGS_DEPENDS  := -c
CFLAGS_DEPENDS  += -pipe
CFLAGS_DEPENDS  += -mcpu=$(ARMCORE)
CFLAGS_DEPENDS  += -fsyntax-only
CFLAGS_DEPENDS  += $(MILB_COMMON_OPT)
CFLAGS_DEPENDS  += $(BOOT_STRAP_OPT)

### Assember
AS  := $(TCHAIN)as
ASFLAGS  := -c
ASFLAGS  += -pipe
ASFLAGS  += -fno-builtin
ASFLAGS  += -ffreestanding
ASFLAGS  += -mtune=cortex-a7
ASFLAGS  += -g3
ASFLAGS  += -march=armv7-a
ifneq "$(FPU_OPTION)" "NO_FPU"
ASFLAGS  += -mfpu=neon-vfpv4
endif
ASFLAGS  += -mfloat-abi=softfp
ASFLAGS  += -mthumb-interwork
ASFLAGS  += -x assembler-with-cpp
ASFLAGS  += $(MILB_COMMON_OPT)
ASFLAGS  += $(BOOT_STRAP_OPT)

ASFLAGS_DEPENDS  := -fsyntax-only
ASFLAGS_DEPENDS  += -pipe
ASFLAGS_DEPENDS  += -mcpu=$(ARMCORE)
ASFLAGS_DEPENDS  += -x assembler-with-cpp
ASFLAGS_DEPENDS  += $(MILB_COMMON_OPT)
ASFLAGS_DEPENDS  += $(BOOT_STRAP_OPT)

##### DELETE_PGRELIEF #####
### PGRelief
ifeq "$(IS_USE_PGRELIEF)" "y"
# Create UNIX path
EPOMDIR_POSIX     = $(shell cygpath --unix "$(EPOMDIR)" | sed -e 's/\\/\\\\/g')

# Create Windows path
PGR_CMD           = "$(EPOMDIR_POSIX)/pgr5.exe"
PGR_TMP           = $(shell cygpath --windows "$(TMP)" | sed -e 's/\\/\\\\/g')
PGR_CURDIR        = $(shell cygpath --windows "$(CURDIR)" | sed -e 's/\\/\\\\/g')
PGR_STDC_INCLUDE1 = $(shell cygpath --windows "$(GNUarmInclude1)" | sed -e 's/\\/\\\\/g')
PGR_STDC_INCLUDE2 = $(shell cygpath --windows "$(GNUarmInclude2)" | sed -e 's/\\/\\\\/g')
PGR_STDC_INCLUDE3 = $(shell cygpath --windows "$(GNUarmInclude3)" | sed -e 's/\\/\\\\/g')
endif

PGRFLAGS          = -c
PGRFLAGS         += --csv
PGRFLAGS         += --output_code SJIS
PGRFLAGS         += --slashcomment
PGRFLAGS         += --C99
PGRFLAGS         += $(CINCPATH)
PGRFLAGS         += -K GNU/GCC3_C90
PGRFLAGS         += -D__GNUC__
PGRFLAGS         += -O "$(PGR_OPT_FILE)"
PGRFLAGS         += -S UTF8
PGRFLAGS         += -Y "$(PGR_STDC_INCLUDE1)"
PGRFLAGS         += -Y "$(PGR_STDC_INCLUDE2)"
PGRFLAGS         += -Y "$(PGR_STDC_INCLUDE3)"
PGRFLAGS         += -Z "$(EPOMDIR)\MessageInfo\default-pgr.rul"
# Workaround of following PG-Relief error.
#   "C:\cygwin\opt\arm-none-eabi\arm-none-eabi\include\machine\ieeefp.h",406,"!","pgr0455","#error  Endianess not declared!!"
PGRFLAGS         += -D__IEEE_LITTLE_ENDIAN
##### DELETE_PGRELIEF #####



#
# Directory and File names
#

OBJ_DIR_NAME     ?= obj
SRC_DIR_NAME     := src
ASM_DIR_NAME     := asm
DEP_DIR_NAME     := dep
INC_DIR_NAME     := include
OBJ_FILE          = $(OBJ_DIR_NAME)/%.o
SRC_CFILE         = $(SRC_DIR_NAME)/%.c
SRC_AFILE         = $(SRC_DIR_NAME)/%.s
DEP_FILE          = $(DEP_DIR_NAME)/%.d
CUROBJSFILE       = $(ROOT_DIR)/$(WS_DIR_NAME)/$(OBJ_DIR_NAME)/$(strip $(subst /,_, $(DIR))).objs
##### DELETE_PGRELIEF #####
PGR_OPT_FILE      = $(ROOT_DIR)/project/boot_strap.pgr
PGR_CSV_FILE      = $(ROOT_DIR)/PGRelief_out.csv
##### DELETE_PGRELIEF #####
CSRC_TMP          = $(patsubst %.c,$(SRC_DIR_NAME)/%.c,$(CSRC))
ASRC_TMP          = $(patsubst %.s,$(SRC_DIR_NAME)/%.s,$(ASRC))
OBJS              = $(CSRC_TMP:$(SRC_DIR_NAME)/%.c=$(OBJ_DIR_NAME)/%.o)
OBJS             += $(ASRC_TMP:$(SRC_DIR_NAME)/%.s=$(OBJ_DIR_NAME)/%.o)
DEPS              = $(CSRC_TMP:$(SRC_DIR_NAME)/%.c=$(DEP_DIR_NAME)/%.d)
DEPS             += $(ASRC_TMP:$(SRC_DIR_NAME)/%.s=$(DEP_DIR_NAME)/%.d)
ALL_SRC           = $(CSRC)
ALL_SRC          += $(ASRC)



#
# Targets
#

QUIET :=

.PHONY: default target-self recursive-target depends-self clean-self distclean-self $(SUBDIRS) \
        target target-objs-self clean clean-obj clean-obj-self clean-depends-self depends message message-self

default: target

target-self: 
ifneq "$(words $(OBJS))" "0"
##### DELETE_PGRELIEF #####
ifeq "$(IS_USE_PGRELIEF)" "y"
	-$(QUIET)if [ ! -d $(TMP)/PGR_output ] ; then mkdir $(TMP)/PGR_output >/dev/null 2>&1; fi
endif
##### DELETE_PGRELIEF #####
	-$(QUIET)$(MAKE) target-objs-self; \
	$(QUIET)$(MAKE) IS_INCLUDE_DEPS=y $(MULTI_JOB_OPT) $(OBJS)
##### DELETE_PGRELIEF #####
ifeq "$(IS_USE_PGRELIEF)" "y"
	-$(QUIET)cat $(OBJ_DIR_NAME)/*_pgr.csv >> $(PGR_CSV_FILE) 2> /dev/null; \
	$(QUIET)rm -f $(OBJ_DIR_NAME)/*_pgr.csv
endif
##### DELETE_PGRELIEF #####
endif

recursive-target: $(PRE_TARGET) $(SUBDIRS) $(POST_TARGET)

depends-self:
ifneq "$(words $(DEPS))" "0"
	-$(QUIET)$(MAKE) clean-depends-self; \
	$(QUIET)$(MAKE) $(MULTI_JOB_OPT) $(DEPS)
endif

clean-self: clean-obj-self clean-depends-self

distclean-self: clean-self

ifeq "$(IS_INCLUDE_DEPS)" "y"
-include $(DEPS)
endif

### Sub Folders
ifneq "$(SUBDIRS)" ""
$(SUBDIRS): 
	$(QUIET)$(MAKE) recursive-target -C $@
endif

### Build
target: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=target-self

target-objs-self: 
ifneq "$(words $(ALL_SRC))" "0"
	-$(QUIET)if [ ! -d $(ROOT_DIR)/$(WS_DIR_NAME)/$(OBJ_DIR_NAME) ] ; then mkdir $(ROOT_DIR)/$(WS_DIR_NAME)/$(OBJ_DIR_NAME); fi; \
	$(QUIET)if [ -f $(CUROBJSFILE) ] ; then rm -f $(CUROBJSFILE); fi; \
	$(QUIET)echo $(patsubst $(OBJ_DIR_NAME)/%.o, $(DIR)/$(OBJ_DIR_NAME)/%.o, $(OBJS)) > $(CUROBJSFILE)
endif

$(OBJ_FILE): $(SRC_CFILE)
	-$(QUIET)if [ ! -d $(OBJ_DIR_NAME) ] ; then mkdir $(OBJ_DIR_NAME) >/dev/null 2>&1; fi; \
	$(QUIET)echo compiling $@ ...; \
	$(QUIET)$(CC) $(CFLAGS) $(CINCPATH) -o $@ $(call PATH_CONVERT, $(CURDIR)/$<)
ifeq "$(IS_GET_ASM)" "y"
	-$(QUIET)if [ ! -d $(ASM_DIR_NAME) ] ; then mkdir $(ASM_DIR_NAME) >/dev/null 2>&1; fi; \
	$(QUIET)$(CC) $(CFLAGS) $(CINCPATH) -S $< -o $(patsubst $(SRC_DIR_NAME)/%.c,$(ASM_DIR_NAME)/%.s,$<)
endif

##### DELETE_PGRELIEF #####
ifeq "$(IS_USE_PGRELIEF)" "y"
	-$(QUIET)$(PGR_CMD) $(PGRFLAGS) --temp-file "$(PGR_TMP)\PGR_output\$(<F)" "$(PGR_CURDIR)\$<" > $(patsubst $(SRC_DIR_NAME)/%.c,$(OBJ_DIR_NAME)/%_pgr.csv,$<)
endif
##### DELETE_PGRELIEF #####

$(OBJ_FILE): $(SRC_AFILE)
	-$(QUIET)if [ ! -d $(OBJ_DIR_NAME) ] ; then mkdir $(OBJ_DIR_NAME) >/dev/null 2>&1; fi; \
	$(QUIET)echo assembling $@ ...; \
	$(QUIET)$(CC) $(ASFLAGS) $(AINCPATH) -o $@ $(call PATH_CONVERT, $(CURDIR)/$<)

### Clean
clean: 
	$(QUIET)$(MAKE) $(MULTI_JOB_OPT) recursive-target POST_TARGET=clean-self

clean-obj: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=clean-obj-self

clean-obj-self: 
	-$(QUIET)if [ -d $(OBJ_DIR_NAME) ] ; then rm -rf $(OBJ_DIR_NAME); fi; \
	$(QUIET)if [ -d $(ASM_DIR_NAME) ] ; then rm -rf $(ASM_DIR_NAME); fi;

clean-depends-self: 
	$(QUIET)if [ -d $(DEP_DIR_NAME) ] ; then rm -rf $(DEP_DIR_NAME); fi;

### Depends
depends: 
	$(QUIET)$(MAKE) recursive-target POST_TARGET=depends-self

$(DEP_FILE): $(SRC_CFILE)
	-$(QUIET)if [ ! -d $(DEP_DIR_NAME) ] ; then mkdir $(DEP_DIR_NAME) >/dev/null 2>&1; fi; \
	$(QUIET)echo creating $@ ...; \
	$(QUIET)$(CC) $(CFLAGS_DEPENDS) $(CINCPATH) -MT $(patsubst $(SRC_DIR_NAME)/%.c,$(OBJ_DIR_NAME)/%.o,$<) -MT $@ -M -MF $@ $< 

$(DEP_FILE): $(SRC_AFILE)
	-$(QUIET)if [ ! -d $(DEP_DIR_NAME) ] ; then mkdir $(DEP_DIR_NAME) >/dev/null 2>&1; fi; \
	$(QUIET)echo creating $@ ...; \
	$(QUIET)$(CC) $(ASFLAGS_DEPENDS) $(AINCPATH) -MT $(patsubst $(SRC_DIR_NAME)/%.s,$(OBJ_DIR_NAME)/%.o,$<) -MT $@ -M -MF $@ $< 


### Message
message: 
	$(QUIET)$(MAKE) recursive-target PRE_TARGET=message-self

message-self: 
ifneq "$(MESSAGE)" ""
	$(QUIET)echo $(MESSAGE)
endif

##### DELETE_PGRELIEF #####
print-compile-option:
	@echo $(BOOT_STRAP_OPT) $(BOOTVERSION_OPT) $(CUSTOM_OPT) $(LOADER_OPT) $(SDRAM_OPT) $(ES_OPT) $(SENSORTYPE_OPT) > $(PGR_OPT_FILE)
##### DELETE_PGRELIEF #####
